package SystemClient.client;


import SystemClient.modal.User;
import SystemClient.modal.UserMsg;

import java.io.IOException;

/**
 * 编程实现管理员系统的功能
 * @author cz
 *
 */
public class ClientManage {
	// 使用合成复用原则
	private Client cic;
	
	/**
	 * 自定义构造方法实现成员变量的初始化
	 * @param cic
	 */
	public ClientManage(Client cic) {
		this.cic = cic;
	}
	
	/**
	 * 自定义成员方法实现管理主界面的绘制
	 * @throws InterruptedException 
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public void clientManagePage() throws InterruptedException, IOException, ClassNotFoundException {
		while(true) {
			System.out.println("\n\n\t    欢迎进入管理员系统");
			System.out.println("------------------------------------------");
			System.out.print("   [1] 学员管理");
			System.out.println("   [0] 退出系统");
			System.out.println("------------------------------------------");
			System.out.println("请选择要进入的管理系统：");
			int choose = ClientScanner.getScanner().nextInt();
			switch(choose) {
				case 1: clientUserManagePage(); break;
				case 0: System.out.println("正在退出管理系统...");
				        Thread.sleep(5000);
				        return;
				default: System.out.println("输入错误，请重新选择！");
			}
		}
	}
	
	/**
	 * 自定义成员方法实现学员管理界面的绘制
	 * @throws InterruptedException 
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public void clientUserManagePage() throws InterruptedException, IOException, ClassNotFoundException {
		while(true) {
			System.out.println("\n\n\t    欢迎进入学员系统");
			System.out.println("------------------------------------------");
			System.out.print("   [1] 增加学员");
			System.out.println("   [0] 退出系统");
			System.out.println("------------------------------------------");
			System.out.println("请选择要进行的操作：");
			int choose = ClientScanner.getScanner().nextInt();
			switch(choose) {
				case 1: clientAddUser(); break;
				case 0: System.out.println("正在退出学员管理系统...");
						Thread.sleep(5000);
						return;
				default: System.out.println("输入错误，请重新选择！");
			}
		}
	}
	
	/**
	 * 自定义成员方法实现客户端增加学员账户的功能
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public void clientAddUser() throws IOException, ClassNotFoundException {
		System.out.println("请输入要增加的学员账户名称：");
		String strName = ClientScanner.getScanner().next();
		// 构造UserMessage类型的对象并使用对象输出流发送给服务器
		UserMsg um = new UserMsg("clientAddUser", new User(strName, "123"));
		cic.getOos().writeObject(um);
		System.out.println("客户端发送增加的账户信息成功！");
		// 接收服务器发来的处理结果并给出提示
		um = (UserMsg) cic.getOis().readObject();
		if("success".equals(um.getType())) {
			System.out.println("增加学员账户成功！");
		} else {
			System.out.println("增加学员账户失败！");
		}
	}
}
